<template>
  <div class="articles-add">
    <el-form
      :model="addArticledata"
      ref="articleRef"
      label-width="80px"
      :rules="rules"
    >
      <el-form-item label="文章标题" prop="title">
        <el-input
          placeholder="请输入文章标题"
          v-model="addArticledata.title"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <quill-editor v-model="addArticledata.articleBody"></quill-editor>
      </el-form-item>
      <el-form-item label="视频地址">
        <el-input
          placeholder="请输入视频地址"
          v-model="addArticledata.videoURL"
        ></el-input>
      </el-form-item>
      <el-form-item size="large">
        <el-button v-on:click="unclearForm">取消</el-button>
        <el-button type="primary" v-on:click="addAticle">添加</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { add } from "@/api/hmmm/articles.js";
export default {
  name: "ArticlesAdd",
  data() {
    return {
      addArticledata: {
        title: "",
        articleBody: "",
        videoURL: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入文章标题", trigger: "blur" },
          {
            min: 5,
            max: 10,
            message: "长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
        articleBody: [
          { required: true, message: "请输入文章内容", trigger: "blur" },
        ],
      },
      errordisplay: false,
    };
  },
  methods: {
    addAticle() {
      this.$refs.articleRef.validate(async (value) => {
        if (!value) {
          return this.$message.error("请填写完整你的表单项");
        } else {
          try {
            await add(this.addArticledata);
            this.$message.success("新增文章成功");
            this.$emit("canceladd");
          } catch (error) {
            this.$message.error("添加文章失败奥", error);
          }
        }
      });
    },
    unclearForm() {
      this.$emit("canceladd");
      this.$refs.articleRef.resetFields();
    },
  },
};
</script>


<style scoped lang='less'>
.articles-add {
  .ql-editor {
    min-height: 300px !important;
  }
}
</style>
